﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using BCCommon;
using BCDto.Trading.Enquiry.SaleEnquiry;
using BCService.Trading.Enquiry.SaleEnquiry;
using BCService.Utils;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace BCTMSRESTful.Controllers.Anonymous
{
    [Route("api/v1/[controller]")]
    [ApiController]
    public class EnquiryController : WebsiteBaseController
    {
        private readonly ISaleEnquiryService saleEnquiryService;

        public EnquiryController(ISaleEnquiryService saleEnquiryService)
        {
            this.saleEnquiryService = saleEnquiryService;
        }

        #region 添加销售询盘
        /// <summary>
        ///  添加销售询盘
        /// </summary>
        /// <param name="requestDto">销售询盘信息</param>
        /// <response code="201">添加销售询盘成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加销售询盘")]
        [ProducesResponseType(typeof(SaleEnquiryDto), 201)]
        public IActionResult Post(
            [FromBody]SaleEnquiryUserRequestDto requestDto)
        {
            var result = this.saleEnquiryService.Add(requestDto);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion
        #region 修改销售询盘
        /// <summary>
        ///  修改销售询盘
        /// </summary>
        /// <param name="saleEnquiryId">销售询盘Id</param>
        /// <param name="requestDto">销售询盘信息</param>
        /// <response code="200">修改销售询盘成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("{sale_enquiry_id}")]
        [Log("修改销售询盘")]
        [ProducesResponseType(typeof(SaleEnquiryDto), 200)]
        public IActionResult Put([FromRoute(Name = "sale_enquiry_id")]long saleEnquiryId, [FromBody]SaleEnquiryStaffRequestDto requestDto)
        {
            SaleEnquiryPutDto putDto = requestDto.As<SaleEnquiryPutDto>();
            putDto.SaleEnquiryId = saleEnquiryId;
            return StatusCode((int)HttpStatusCode.OK, this.saleEnquiryService.Update(putDto));
        }
        #endregion

        #region 删除销售询盘
        /// <summary>
        ///  删除销售询盘
        /// </summary>
        /// <param name="saleEnquiryId">销售询盘Id</param>
        /// <response code="204">删除销售询盘成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("{sale_enquiry_id}")]
        [Log("删除销售询盘")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "sale_enquiry_id")]long saleEnquiryId)
        {
            this.saleEnquiryService.Delete(saleEnquiryId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取销售询盘
        /// <summary>
        ///  获取销售询盘
        /// </summary>
        /// <param name="saleEnquiryId">销售询盘Id</param>
        /// <response code="200">获取销售询盘</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{sale_enquiry_id}")]
        [ProducesResponseType(typeof(SaleEnquiryDto), 200)]
        public IActionResult Get([FromRoute(Name = "sale_enquiry_id")]long saleEnquiryId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleEnquiryService.Get(saleEnquiryId));
        }
        #endregion

        #region 获取销售询盘（分页）
        /// <summary>
        ///  获取销售询盘（分页）
        /// </summary>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取销售询盘（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<SaleEnquiryDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleEnquiryService.Query(this.UserInfo.UserId, start, end, sortName, ascending, index, size));
        }
        #endregion

    }
}
